package org.example.loadbalancer;

import org.example.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @description TODO
 * @author sats@jz
 * @date 2025年01月13日 14:18
 */
public class RandomLoadBalancer implements LoadBalancer{
    private final Random random = new Random();
    /**
     * @email  jzhouwang@163.com
     * @author  sats
     * @date  2025/1/13 14:26
     * @param requestParams
     * @param serviceMetaInfos
     * @return org.example.model.ServiceMetaInfo
     * 随机选取
     */
    @Override
    public ServiceMetaInfo select(Map<String, Object> requestParams, List<ServiceMetaInfo> serviceMetaInfoList) {
        if (serviceMetaInfoList.isEmpty()){
            return null;
        }

        // 只有一个服务不需要轮询
        int size = serviceMetaInfoList.size();
        if(size == 1){
            return serviceMetaInfoList.get(0);
        }


        return serviceMetaInfoList.get(random.nextInt(size));
    }
}
